<?php
/**
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 16/8/20
 * Time: 下午9:10
 */

namespace Superadmin\Controller;


use Think\Upload;

class DownloadController extends BaseController
{

    private  $mod;
    public function __construct()
    {
        parent::__construct();
        $this->mod = D('download');
    }

    public function index()
    {
        $big_menu = array(
            'title' => L('新文件'),
            'uri' => U('download/add'),
        );
        $this->assign('big_menu', $big_menu);

        $list = $this->getpage($this->mod, array('status' =>1), 'ordid asc', C('page_size'));
        $this->assign('list', $list['list']);
        $this->assign('page', $list['page']);
        $this->display();
    }

    public function add()
    {
        if(IS_AJAX)
        {
            $params = $_POST;
            if(isset($params['img']) && $params['img'])
            {
                foreach($params['img'] as $key => $val)
                {
                    $val['add_time'] = time();
                    M('download')->add($val);
                }
            }
            $this->ajaxReturn(array('status' => 1, 'msg' => '保存成功'));
        }
        $this->display();
    }

    public function upload()
    {
        $upload = new Upload();// 实例化上传类
        $upload->maxSize   =     200 * 1024 * 1024 ;// 设置附件上传大小
        $upload->exts      =     array('zip','docx', 'doc', 'xls', 'xlsx', 'mp3', 'mp4', 'jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './upload/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $back = array(
                'status' => 0,
                'msg'   => $upload->getError(),
            );
            $this->ajaxReturn($back);
        }else{// 上传成功
            $back = array(
                'status' => 1,
                'data'   => $info,
            );
            $this->ajaxReturn($back);
        }
    }



    public function delete()
    {
        if(IS_AJAX){
            $id = I('get.id');
            $this->mod->where(array('id' => $id))->delete();
            $back = array(
                'status' => 1,
                'msg'    => L('operation_success')
            );
            $this->ajaxReturn($back);
        }

    }


}